//
//  CHTableHeaderView.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/11.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHTableHeaderView.h"
@interface CHTableHeaderView()
{
    
    BOOL isOpen;
}

@property (nonatomic ,strong) UIButton *titleBtn;
@property (nonatomic ,strong) UILabel *resultLabel;
@property (nonatomic ,strong) UIImageView *iconImgView;

@end
@implementation CHTableHeaderView



-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        titleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [titleBtn setTitleColor:[UIColor colorWithHexString:@"333333" alpha:1.0f] forState:UIControlStateNormal];
        // 设置按钮的内容居左显示
        titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        // 设置按钮的内容全部往右10有间隙
        titleBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        
        // 添加监听事件
        [titleBtn addTarget:self action:@selector(nameViewClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:titleBtn];
        self.titleBtn = titleBtn;
        
        
        UILabel *resultLabel = [[UILabel alloc] init];
        resultLabel.textColor = [UIColor colorWithHexString:@"333333" alpha:1.0f];
        resultLabel.font = [UIFont systemFontOfSize:14];
        resultLabel.textAlignment = NSTextAlignmentRight;
        resultLabel.numberOfLines = 0;
        self.resultLabel = resultLabel;
        [self addSubview:resultLabel];
        
        self.iconImgView = [[UIImageView alloc] init];
        self.iconImgView.image = [UIImage imageNamed:@"right_arrow"];
        [self addSubview:self.iconImgView];
        self.iconImgView.hidden = YES;
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self addGestureRecognizer:tap];
        
        
    }
    return self;
    
}


-(void)tapAction{
    

    self.model.isOpened = !self.model.isOpened;
    
    if (self.block) {
        self.block();
    }
    
}


-(void)setModel:(JLDetailModel *)model{
    _model = model;
   [self.titleBtn setTitle:model.title forState:UIControlStateNormal];
    
    if ([model.title isEqualToString:@"结算日期"] || [model.title isEqualToString:@"送修日期"]) {
        self.resultLabel.text = [model.detailTitle substringToIndex:11];
    }else if ([model.title isEqualToString:@"送修里程"]) {
        self.resultLabel.text = [NSString stringWithFormat:@"%@公里",model.detailTitle];
    }else{
        self.resultLabel.text = model.detailTitle;
    }
    if ([model.title isEqualToString:@"维修项目"] || [model.title isEqualToString:@"维修配件"]) {
        self.iconImgView.hidden = NO;
        self.resultLabel.hidden = YES;
        
    }else{
        self.iconImgView.hidden = YES;
        self.resultLabel.hidden = NO;
    }
}


- (void)nameViewClick
{
    self.model.isOpened = !self.model.isOpened;
    // 设置箭头状态
    if (self.model.isOpened) {
        self.iconImgView.transform = CGAffineTransformMakeRotation(M_PI_2);
    } else {
        self.iconImgView.transform = CGAffineTransformIdentity;
    }
    if (self.block) {
        self.block();
    }
}

-(void)willMoveToSuperview:(UIView *)newSuperview{
    
    // 设置箭头状态
    if (self.model.isOpened) {
        self.iconImgView.transform = CGAffineTransformMakeRotation(M_PI_2);
    } else {
        self.iconImgView.transform = CGAffineTransformIdentity;
    }
    
}

/**
 *  当一个控件被添加到父控件上以后调用的方法
 *  我们需要在 新的headerView被添加到tabelView以后，再旋转当前headerView的箭头
 */
- (void)didMoveToSuperview
{
    // 3. 让箭头旋转
    if (self.model.isOpened) { // 展开
        self.iconImgView.transform = CGAffineTransformMakeRotation(M_PI_2); // 90°
    }else{
        self.iconImgView.transform = CGAffineTransformMakeRotation(0); // 还原位置
    }
}



-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.titleBtn.frame = self.bounds;
    
    // frame
    CGFloat padding = 10;
    CGFloat onlineY = 0;
    CGFloat onlineH = self.bounds.size.height;
    CGFloat onlineW = UIScreenW/2;
    CGFloat onlineX = self.bounds.size.width - padding - onlineW;
    
    self.resultLabel.frame = CGRectMake(onlineX, onlineY, onlineW, onlineH);
    self.iconImgView.frame = CGRectMake(UIScreenW - 15 -10, (self.h - 10) /2, 10, 10);
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"eeeef0" alpha:1];
    [self addSubview:line];
    
    
    
}


@end
